package net.expectx.gw.aliyun;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


public class AlipayUtil {
    private static String appId;

    private static String merchantPrivateKey;
    private static String alipayPublicKey;
    private static String notifyUrl ;
    private static String returnUrl ;
    private static String signType ;
    private static String charset = "utf-8";
    private static String gatewayUrl ;


    public static void config(String app_id,String merchant_private_key,String alipay_public_key,String notify_url,String return_url,String sign_type,String gateway_url ) {
        appId=app_id;
        merchantPrivateKey = merchant_private_key;
        alipayPublicKey=alipay_public_key;
        notifyUrl=notify_url;
        returnUrl=return_url;
        signType=sign_type;
        gatewayUrl=gateway_url;
    }
    /**
     * 阿里支付
     * @param request
     * @param response
     */
    public static void pay(HttpServletRequest request, HttpServletResponse response){
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out=response.getWriter();
            /**
             * 获得初始化的AlipayClient
             */
            AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, merchantPrivateKey, "json", charset, alipayPublicKey, signType);

            /**
             * 设置请求参数
             */
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(returnUrl);
            alipayRequest.setNotifyUrl(notifyUrl);

            /**
             * 商户订单号，商户网站订单系统中唯一订单号，必填
             */
            String outTradeNo = new String(request.getParameter("order_id"));
            String totalAmount = new String(request.getParameter("product_price"));
            /**
             * 订单名称，必填
             */
            String subject = new String(request.getParameter("product_name"));
            /**
             * 商品描述，可空
             */
            String body = new String(request.getParameter("product_desc"));

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ outTradeNo +"\","
                    + "\"total_amount\":\""+ totalAmount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"body\":\""+ body +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
            //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
            //		+ "\"total_amount\":\""+ total_amount +"\","
            //		+ "\"subject\":\""+ subject +"\","
            //		+ "\"body\":\""+ body +"\","
            //		+ "\"timeout_express\":\"10m\","
            //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

            String result = alipayClient.pageExecute(alipayRequest).getBody();
            out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
